package service.center.run;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import service.center.message.易源天气;
import service.center.message.阿里云短信;
import service.center.mysql.User;
import service.center.mysql.UserMapper;

import java.util.List;

@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
    @Autowired
    UserMapper userMapper;

    @Override
    public void run(ApplicationArguments args)  {
        System.out.println("通过实现ApplicationRunner接口，在springboot项目启动后执行");
        List<User> users= userMapper.selectall();
        String temp;
        String 提醒内容json;
        for (User user : users) {
            //首先判断下该用户是否有发送余额
            if (user.get剩余次数()==1){
                System.out.println(user.get称呼()+"剩余次数不足，不再发送");
                continue;
            }
            temp= 易源天气.经纬度查天气(user.get经度(), user.get纬度());
            提醒内容json = 易源天气.经纬度查天气解析(temp, user.get称呼()).replace("划船","出行");


            if (提醒内容json.indexOf("降雨概率0%")==-1||"全部".equals(user.get雨天发送())){
                temp = 阿里云短信.天气提醒(user.get手机号(), 提醒内容json);
                userMapper.deletedone(user.get手机号());
                System.out.println("已经向"+user.get称呼()+"发送提醒短信，剩余次数"+(user.get剩余次数()-1));
            }
        }

        System.out.println("执行任务结束，即将退出springboot项目");
        System.exit(0);
    }
}
